

package de.global42.clubapp.rest;


import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import javax.interceptor.Interceptors;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import de.global42.clubapp.ejbs.ClientCouponHandler;
import de.global42.clubapp.ejbs.ClientMissionHandler;
import de.global42.clubapp.ejbs.ClientRegistrator;
import de.global42.clubapp.interceptors.RESTfulInterceptor;
import de.global42.clubapp.model.Coupon;
import de.global42.clubapp.model.Mission;
import de.global42.clubapp.util.QueryParamMap;

/**
 * @author dominik
 *
 */
@RequestScoped
@Named("MobileClientService")
@Path("/webservice")
@Interceptors(RESTfulInterceptor.class)
public class MobileClientService {
	@EJB
	private ClientRegistrator clientRegistrator;
	@EJB
	private ClientMissionHandler clientMissionHandler;
	@EJB
	private ClientCouponHandler clientCouponHandler;
	
	@Context
	private Response response;
	
	@Path("register")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public String register() {
		return clientRegistrator.register(); //TODO pass token to verify valid registration!
	}
	
	@Path("test")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public String test(@Context UriInfo uriInfo, QueryParamMap parameterMap) {
		respondServerError();
		return "";
	}
	
	@Path("getmissions")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Map<Long, Set<Mission>> getMissions(@Context UriInfo uriInfo, QueryParamMap parameterMap){
		Long clientId = parameterMap.getLong("client");
		Date lastUpdatedDate = parameterMap.getDateFromTimeStamp("lastupdated");
		
		return clientMissionHandler.getMissions(clientId , lastUpdatedDate );
	}
	
	private void respondServerError() {
		response.serverError();
	}

	@Path("getclientmissions")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Map<Long, Object> getClientMissions(@Context UriInfo uriInfo, QueryParamMap parameterMap){
		
		Long clientId = parameterMap.getLong("client");
		return clientMissionHandler.getClientMissions(clientId);
	}
	
	@Path("getclientcoupons")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public List<Coupon> getClientCoupons(@Context UriInfo uriInfo, QueryParamMap parameterMap){
		Long clientId = parameterMap.getLong("client");
		return clientCouponHandler.getClientCoupons(clientId);
	}
	
	@Path("entervalidationcode")
	@PUT
	@Produces(MediaType.APPLICATION_JSON)
	public String enterValidationCode(@Context UriInfo uriInfo, QueryParamMap parameterMap){
		Long clientId = parameterMap.getLong("client");
		String validationCode = parameterMap.get("validationcode");
		Long venueId = parameterMap.getLong("venue");
		return clientMissionHandler.enterValidationCode(clientId,validationCode,venueId);
	}
	
	@Path("devaluatecoupon")
	@PUT
	@Produces(MediaType.APPLICATION_JSON)
	public String devaluateCoupon(@Context UriInfo uriInfo, QueryParamMap parameterMap){
		Long clientId = parameterMap.getLong("client");
		Long couponId = parameterMap.getLong("coupon");
		return clientCouponHandler.devaluateCoupon(clientId,couponId);
	}
	
//	@EJB
//	public void setClientRegistrator(ClientRegistrator registrationBean) {
//		this.clientRegistrator = registrationBean;
//	}
//	@EJB
//	public void setClientMissionHandler(ClientMissionHandler clientMissionHandler) {
//		this.clientMissionHandler = clientMissionHandler;
//	}
//	@EJB
//	public void setClientCouponHandler(ClientCouponHandler clientCouponHandler) {
//		this.clientCouponHandler = clientCouponHandler;
//	}

}
